package com.wandian.audio.mvvm.audiomusicrankdj;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.R;
import com.wandian.audio.adapter.AudioMusicRankRecycleAdapter;
import com.wandian.audio.bean.MusicBean;
import com.wandian.audio.databinding.AudioActivityMusicRankDjBinding;
import com.wandian.audio.tools.MusicCollector;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description dj音乐排行activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_AUDIO_MUSIC_RANK_DJ)
public class AudioMusicDJRankActivity extends BaseMVVMActivity<AudioMusicDJRankActivityViewModel> {
    private AudioActivityMusicRankDjBinding dataBinding;
    private AudioMusicRankRecycleAdapter audioMusicRankRecycleAdapter;
    private String title;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.audio_activity_music_rank_dj);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        title = getIntent().getStringExtra("title")+"";
        initRecycle();
        initViewData();
        initClink();
        mViewModel.getMusicRank(title);
    }

    private void initRecycle(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.recycler.setLayoutManager(layoutManager);
        audioMusicRankRecycleAdapter  = new AudioMusicRankRecycleAdapter(R.layout.audio_item_rank_style_one);
        dataBinding.recycler.setAdapter(audioMusicRankRecycleAdapter);
        audioMusicRankRecycleAdapter.setOnItemClickListener((adapter,view,position)->{
            MusicBean musicBean = (MusicBean) adapter.getData().get(position);
            if(!UIUtils.isEmpty(musicBean)){
                Bundle bundle = new Bundle();
                bundle.putString("music_type","network");
                bundle.putString("music_id",musicBean.getId());
                ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_AUDIO_MAIN).with(bundle).navigation();
            }
        });
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        switch (title){
            case "人气榜":
                dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("人气榜");
                break;
            case "新歌榜":
                dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("新歌榜");
                break;
            case "收藏榜":
                dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("收藏榜");
                break;
            case "今日推荐":
                dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("今日推荐");
                break;
        }

        mViewModel.listData.observe(this,rankBean->{
            if(!UIUtils.isEmpty(rankBean)){
                MusicCollector.clearAllSong();
                MusicCollector.setSongsList(mViewModel.listData.getValue());
                audioMusicRankRecycleAdapter.setNewData(rankBean);
            }
        });
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });
    }



}
